// 去重是是这道题的难点
var threeSum = function(nums) {
    let arr = [];
    nums.sort((a,b)=>a-b);
    // console.log(nums,"sortarr"); 
    for(let i = 0;i<nums.length;i++) {
        if(nums[i] > 0) break; // 如果当前数字大于0，则三数之和一定大于0，所以结束循环
        if(i > 0 && nums[i] == nums[i-1]) continue; // 去重
        let left = i+1;
        let right = nums.length-1;
        if(left>=right) {
            break;
        }
        while(left<right) {
            let sum = nums[i] + nums[left] + nums[right];
            if(sum===0) {
                arr.push([nums[i],nums[left],nums[right]]);
                while (left<right && nums[left] == nums[left+1]) left++; // 去重
                while (left<right && nums[right] == nums[right-1]) right--; // 去重
                left++;
                right--;
            }else if(sum > 0) {
                right--;
            }else {
                left++;
            }
        }
    }
    console.log(arr,"--arr");
};

threeSum([-1,0,1,2,-1,-4])